package com.example.demo.retrofit;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.Retrofit.Builder;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.Observable;
import rx.Observer;
import rx.Subscriber;
import rx.Subscription;
import rx.functions.Func1;
import rx.functions.Function;
import rx.schedulers.Schedulers;

import java.util.Map;

public class RetrofitUtil {
    public static Retrofit getInstant(){
        return new Builder().baseUrl(BaseApiConstant.BASE_URI).addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create()).build();
    }

    public static void main(String[] args) {
        ApiService apiService = RetrofitUtil.getInstant().create(ApiService.class);
        Observable<String> observable1 = apiService.initCode().flatMap((Func1<Map, Observable<String>>) o -> {
            return Observable.just((String)o.get("img"));
        });
        Subscriber subscriber = new Subscriber<Object>() {
            @Override
            public void onCompleted() {
                System.out.println("onCompleted");
            }

            @Override
            public void onError(Throwable throwable) {
                System.out.println("onError");
            }

            @Override
            public void onNext(Object o) {
                System.out.println("onNext");
                System.out.println(o);
            }
        };

        Subscription subscribe = observable1.subscribe(subscriber);
        subscribe.unsubscribe();
    }
}
